/*
 * @(#) PartInterService.java 2013-8-6 下午1:22:14
 *
 * Copyright 2013 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.osi.mes.inter.erp;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

import pnuts.lang.Context;

import com.datasweep.compatibility.client.BillOfMaterials;
import com.datasweep.compatibility.client.BomItem;
import com.datasweep.compatibility.client.Order;
import com.datasweep.compatibility.client.Part;
import com.datasweep.compatibility.client.Response;
import com.datasweep.compatibility.pnuts.Functions;
import com.osi.mes.inter.BaseInterService;
import com.osi.mes.pm.object.UIOrder;
import com.rockwell.ftpc.framework.common.XMLUtils;
import com.rockwell.ftpc.framework.common.XmlData;
import com.rockwell.mes.rtmis.middle.constants.IMDConstants;
import com.rockwell.ftpc.framework.common.DateTimeUtils;

public class WorkOrderInterService extends BaseInterService
{

	private static WorkOrderInterService instatnce = null;

	private WorkOrderInterService(Context context)
	{
		super(context);
	}

	private WorkOrderInterService(Functions functions)
	{
		super(functions);
	}

	public static WorkOrderInterService getInstance(Context context)
	{
		if (instatnce == null)
		{
			synchronized (WorkOrderInterService.class)
			{
				if (instatnce == null)
				{
					instatnce = new WorkOrderInterService(context);
				}
			}
		}
		return instatnce;
	}

	public Response sendWorkOrderToSAP(Order order, String operation)
	{
		Response response = new Response();
		try
		{
			Hashtable sendData = new Hashtable();
			sendData.put(
				IMDConstants.MD_TABLE_COLUMN_CREATE_DATE, DateTimeUtils.formatDate(
					getFunctions().getDBTime(), "yyyy-MM-dd HH:mm:ss"));
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSGObJ_TYPE, "202");
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSGObJ_DESC, "Work Order");
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSG_TRANS_TYPE, operation);
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSG_KEY_FIELDS, "DocNum");
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSG_FIELDS_VALUE, order.getOrderNumber());

			Hashtable<String, ArrayList<Object>> orderData = convertOrderToHashTable(order);
			String str = XMLUtils.toXMLStr(orderData);

			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSG_LENGTH, str.length());
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSG_CONTENT, str);
			Hashtable responseData = (Hashtable) getFunctions().subroutine(
				"RTMIS_Middle_MESSendDate", sendData);
			int status = (Integer) responseData.get(IMDConstants.RESPONSE_STATUS);
			if (status == IMDConstants.RESPONSE_STATUS_FAILED)
			{
				response = createErrorResponse(new Exception(
					(String) responseData.get(IMDConstants.RESPONSE_LAST_ERR_MSG)));
			}
		}
		catch (Exception e)
		{
			response = createErrorResponse(e);
		}
		return response;
	}

	/**
	 * 测试用方法
	 */
	public Response sendWorkOrderToSAP_()
	{
		Response response = new Response();
		try
		{
			Hashtable sendData = new Hashtable();
			sendData.put(
				IMDConstants.MD_TABLE_COLUMN_CREATE_DATE, DateTimeUtils.formatDate(
					getFunctions().getDBTime(), "yyyy-MM-dd HH:mm:ss"));
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSGObJ_TYPE, "202");
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSGObJ_DESC, "MES Test");
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSG_TRANS_TYPE, "ADD");
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSG_KEY_FIELDS, "DocNum");
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSG_FIELDS_VALUE, "ORD20130612P110127");

			Hashtable<String, ArrayList<Object>> orderDataMain = new Hashtable<String, ArrayList<Object>>();
			Hashtable<String, ArrayList<Object>> orderData = new Hashtable<String, ArrayList<Object>>();

			orderData.put(
				"DocNum", toTransferValue("ORD20130612P110127"));
			orderData.put(
				"SubType", toTransferValue("中文"));
			orderData.put(
				"SplitMthd", toTransferValue(" "));
			orderData.put(
				"PostDate", toTransferValue("2013/06/03 22:33:44"));
			orderData.put(
				"DueDate", toTransferValue("2013/06/05 11:22:33"));
			orderData.put(
				"ItemCode", toTransferValue("109088"));
			orderData.put(
				"PlannedQty", toTransferValue("2000.0"));
			orderData.put(
				"Ref1", toTransferValue(" "));
			orderData.put(
				"Ref2", toTransferValue(" "));
			orderData.put(
				"Comments", toTransferValue(" "));

			ArrayList<Object> linesData = new ArrayList<Object>();

			for (int i = 0; i < 2; i++)
			{
				Hashtable<String, ArrayList<Object>> bomItemData = new Hashtable<String, ArrayList<Object>>();
				bomItemData.put(
					"ItemCode", toTransferValue("20202" + i));
				bomItemData.put(
					"PlannedQty", toTransferValue(1000 * (i + 1)));

				linesData.add(bomItemData);
			}
			orderData.put(
				"Lines", linesData);
			orderDataMain.put(
				"ProductionOrder", toTransferValue(orderData));

			String str = toSendXML(XMLUtils.toXMLStr(orderDataMain));
			System.out.println(str);

			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSG_LENGTH, str.length());
			sendData.put(
				IMDConstants.MD_TABLE_COULMN_MSG_CONTENT, str);
			Hashtable responseData = (Hashtable) getFunctions().subroutine(
				"RTMIS_Middle_MESSendDate", sendData);

		}
		catch (Exception e)
		{
			response = createErrorResponse(e);
		}
		return response;
	}

	/**
	 * 将工单转换成Hashtable
	 */
	private Hashtable<String, ArrayList<Object>> convertOrderToHashTable(Order order)
	{

		Hashtable<String, ArrayList<Object>> orderData = new Hashtable<String, ArrayList<Object>>();

		UIOrder uiOrder = new UIOrder(order, getFunctions());
		orderData.put(
			"DocNum", toTransferValue(uiOrder.getOrderName()));
		orderData.put(
			"SubType", toTransferValue(uiOrder.getOrderType()));
		orderData.put(
			"SplitMthd", toTransferValue(uiOrder.getMaterialSplitMethod()));
		orderData.put(
			"PostDate", toTransferValue(order.getCreationTime()));
		orderData.put(
			"DueDate", toTransferValue(uiOrder.getPlannedTime()));
		orderData.put(
			"ItemCode", toTransferValue(uiOrder.getPart().getPartNumber()));
		orderData.put(
			"PlannedQty", toTransferValue(uiOrder.getPlannedQuantity()));

		ArrayList<Object> linesData = new ArrayList<Object>();

		BillOfMaterials billOfMaterials = uiOrder.getBom();
		Vector bomItems = billOfMaterials.getBomItems();

		for (int i = 0; i < bomItems.size(); i++)
		{
			BomItem bomItem = (BomItem) bomItems.get(i);
			Hashtable<String, ArrayList<Object>> bomItemData = new Hashtable<String, ArrayList<Object>>();
			bomItemData.put(
				"ItemCode", toTransferValue(bomItem.getPartNumber()));
			bomItemData.put(
				"PlannedQty", toTransferValue(bomItem.getPreciseQuantity().floatValue()));

			linesData.add(bomItemData);
		}
		orderData.put(
			"Lines", linesData);

		return orderData;
	}
}
